package com.example.shkjdemo

import android.app.Activity
import android.content.pm.PackageManager
import android.hardware.Camera
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shkjdemo.adapter.MainAdapter
import com.example.shkjdemo.bean.MainMenuBean
import com.orhanobut.logger.Logger
import java.util.*


class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity..."
    lateinit var mAdapter: MainAdapter
    lateinit var recyclerView: RecyclerView
    var mListData: MutableList<MainMenuBean> = arrayListOf()

    lateinit var mActivity: Activity
    private var PERMISSIONS = arrayOf(
        "android.permission.CAMERA",
        "android.permission.READ_EXTERNAL_STORAGE",
        "android.permission.WRITE_EXTERNAL_STORAGE",
        "android.permission.ACCESS_FINE_LOCATION",
        "android.permission.ACCESS_COARSE_LOCATION"
    )

    private var mListener: PermissionListener? = null

    init {
        initMenu()
        mAdapter = MainAdapter(this, mListData)
        mAdapter.notifyDataSetChanged()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mActivity = this
        recyclerView = findViewById(R.id.recyclerView)
        recyclerView.adapter = mAdapter
        val layoutManager: RecyclerView.LayoutManager = GridLayoutManager(this, 2)
        recyclerView.layoutManager = layoutManager

        var cameraNum = Camera.getNumberOfCameras()
        var info = Camera.CameraInfo()
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            println("$TAG 是前置摄像头")
        } else if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
            println("$TAG 是后置摄像头")
            var cInfo = Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info)
            println(TAG + cInfo.toString())
        } else {
            println("$TAG 其他")
        }
        requestRuntimePermissions(PERMISSIONS, object : PermissionListener {
            override fun granted() {
// 权限申请通过
                Toast.makeText(mActivity, "权限申请通过", Toast.LENGTH_SHORT).show()
            }

            override fun denied(deniedList: List<String>) {

            }
        })
    }

    private fun initMenu() {
        mListData.add(MainMenuBean(1, "Camera1"))
        mListData.add(MainMenuBean(2, "Camera2"))
        mListData.add(MainMenuBean(3, "CameraX"))
        mListData.add(MainMenuBean(4, "录音demo"))
        mListData.add(MainMenuBean(5, "触摸事件"))
        mListData.add(MainMenuBean(6, "横向List"))
        mListData.add(MainMenuBean(7, "滚动二级联动"))
        mListData.add(MainMenuBean(8, "json动画"))
        mListData.add(MainMenuBean(9, "蓝牙demo"))
        mListData.add(MainMenuBean(10, "跨进程传递参数"))
        mListData.add(MainMenuBean(11, "拍题识题"))
        mListData.add(MainMenuBean(12, "图片剪切"))
        mListData.add(MainMenuBean(13, "波纹动画"))
        mListData.add(MainMenuBean(100, "退出应用"))
        val own: String? = UUID.randomUUID().toString()
        Logger.v("initMenu: $own")

    }


    private fun requestRuntimePermissions(arr: Array<String>?, listener: PermissionListener) {
        mListener = listener
        val permissionList = mutableListOf<String>()
        for (permission in arr!!) {
            println(permission)
            if (ContextCompat.checkSelfPermission(
                    mActivity,
                    permission
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                println("2...$permission")
                permissionList.add(permission)
            }
        }


        // 申请权限
        if (permissionList.isNotEmpty()) {
            ActivityCompat.requestPermissions(
                this,
                permissionList.toTypedArray(), 1
            )
        }
        if (mListener != null) {
            mListener!!.granted()
        }


    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        if (grantResults.isNotEmpty()) {
            val deniedList = mutableListOf<String>()
            // 遍历所有申请的权限，把被拒绝的权限放入集合
            for (i in grantResults.indices) {
                var grantResult = grantResults[i]
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    if (mListener != null) {
                        mListener!!.granted()
                    }
                } else {
                    deniedList.add(permissions[i])
                }
            }
            if (deniedList.isNotEmpty()) {
                if (mListener != null) {
                    mListener!!.denied(deniedList)
                }

            }
        }

    }
}

